package com.mdc.mansion.core.common.translate.util;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.MessageFormat;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * 调用google 翻译接口
 * 
 * @author zhaoyuchun
 */
public class TranslateUtil {
    private static final String URL_TEMPLATE = "http://translate.google.cn/?langpair={0}&text={1}";
    private static final String ID_RESULTBOX = "result_box";
    private static final String ENCODING = "UTF-8";
    private static final String AUTO = "auto"; // 自动识别
    private static final String CHINA = "zh-CN"; // 中文简体
    private static final String ENGLISH = "en"; // 英
    private static final String ID = "id"; // 印尼
    private static String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7";
    public static String translate(final String text, final String target_lang) throws Exception {
        return translate(text, AUTO, target_lang);
    }
    /**
     * 中文到英文
     * @date create_time：2018年1月14日 上午11:46:41
     * @param text
     * @return
     * @throws Exception
     */
    public static String cn2en(String text) throws Exception{
        return translate(text, CHINA, ENGLISH);
    }
    /**
     * 中文到印尼文
     * @date create_time：2018年1月14日 上午11:46:41
     * @param text
     * @return
     * @throws Exception
     */
    public static String cn2id(String text) throws Exception{
        return translate(text, CHINA, ID);
    }
    /**
     * 英文到中文
     *  @date create_time：2018年1月14日 上午11:46:41
     * @param text
     * @return
     * @throws Exception
     */
    public static String en2cn(String text) throws Exception{
        return translate(text, ENGLISH, CHINA);
    }
    /**
     * 英文到印尼文
     *  @date create_time：2018年1月14日 上午11:46:41
     * @param text
     * @return
     * @throws Exception
     */
    public static String en2id(String text) throws Exception{
        return translate(text, ENGLISH, ID);
    }
    /**
     * 印尼文到中文
     *  @date create_time：2018年1月14日 上午11:46:41
     * @param text
     * @return
     * @throws Exception
     */
    public static String id2cn(String text) throws Exception{
        return translate(text, ID, CHINA);
    }
    /**
     * 印尼文到英文
     *  @date create_time：2018年1月14日 上午11:46:41
     * @param text
     * @return
     * @throws Exception
     */
    public static String id2en(String text) throws Exception{
        return translate(text, ID, ENGLISH);
    }
    public static String translate(final String text, final String src_lang, final String target_lang) throws Exception {
        InputStream is = null;
        Document doc = null;
        Element ele = null;
        try {
            String url = MessageFormat.format(URL_TEMPLATE, URLEncoder.encode(src_lang + "|" + target_lang, ENCODING), URLEncoder.encode(text, ENCODING));
            is = (InputStream)download(url);
            doc = Jsoup.parse(is, ENCODING, "");
            ele = doc.getElementById(ID_RESULTBOX);
            String result = ele.text();
            return result;

        } finally {
            IOUtils.closeQuietly(is);
            is = null;
            doc = null;
            ele = null;
        }
    }
    public static Object download(final String url) throws Exception {
        Exception err = null;
        HttpRequestBase request = null;
        HttpResponse response = null;
        HttpEntity entity = null;
        Object result = null;
        HttpClient httpclient  = HttpClients.createDefault();
        try {
            request = new HttpGet(url);
            request.addHeader("User-Agent", userAgent);
            response = httpclient.execute(request);
            entity = response.getEntity();
            entity = new BufferedHttpEntity(entity);
            result = new BufferedInputStream(entity.getContent());
            return result;
        } catch (Exception e) {
            err = e;
            return null;
        } finally {
            request = null;
            response = null;
            entity = null;
            if (err != null) {
                throw err;
            }
        }
    }

    /**
     * @author liuxiang
     * @throws Exception
     * @date create_time：2018年1月14日 上午12:01:41
     */
    public static void main(String[] args) throws Exception {
//        String str = translate("蛋炒饭", ENGLISH);
        String str = en2cn("Fried rice");
        System.out.println(str);
    }
}